// A magic wand casts a particular type of spell
// It has it's own supply of magic points (mps)
//
// It can be recharged, although this will occasionally destroy the wand
//
// If broken, the wand will fire it's ramianing charge randomly

package game.jrogue;

import java.util.*;

public class Wand  {
    // 27-08-04
	// added new names for the wands,
	// names supplied by DragonLady.
    private static String[] wandtypes= {"버드나무",
    		"상수리나무","호두나무","벚나무","느릅나무",
			"아카시아나무","유칼립투스나무","금속",
			"상아","샌들나무","수정",
			"상수리나무","흑단","헤이즐나무",
			"불사조깃장식","유니콘 뿔",
			"일각고래 뿔","은도금",
			"금도금","청동도금",
			"구리도금","미스릴도금",
			"물푸레나무","로웬나무","삼나무","느티나무",
			"포플러나무","소나무","트렌트나무",
			"사과나무","배나무","마호가니",
			"뼈","호박석","경옥",
			"대나무","무쇠","청동","구리",
			"미스랄","은","금","녹색",
			"붉은색","푸른색","검은색","흰색",
			"노란색","주황색","보라색",
			"남색","무지개빛"
			};
    private static String[] wandAdj = {
    		"휘어진", 
			"문양새겨진", 
			"녹슨", 
			"밋밋한", 
			"끈적끈적한", 
			"반짝거리는", 
			"둔탁한", 
			"갈라진", 
			"구부러진", 
			"광택나는", 
			"얇은", 
			"두꺼운", 
			"무거운", 
			"가벼운", 
			"큰 보석이 박힌", 
			"작은 보석이 박힌",
			"소용돌이모양으로 구부러진", 
			"길다란", 
			"짧은", 
			"깃털달린", 
			"비늘달린"};
    private static int[]    images   = {288,
    		288,288,288,288,288,288,290,288,
			288,289,288,290,288,288,288,288,
			290,290,290,290,290,288,288,288,
			290,288,288,288,288,288,288,288,
			290,289,289,288,290,290,290,290,
			290,290,289,289,289,289,289,289,
			289,289,289,289};
    
    
    private static class WandScript extends Script {
    	private static final long serialVersionUID = 8376119664408202073L;

        public boolean handle(Thing t, Event e) {
    		int charges=t.getStat("Charges");
    		Thing user=e.getThing("사용자");
    		if (charges>0) {
    			user.message("당신은 "+Text.krSub(t.getTheName(), "을")+" 흔듭니다.");
    			Thing s=Spell.create(t.getString("WandSpell"));
    			QuestApp.getInstance().getScreen().castSpell(user,s);
    			Item.identify(t);
    			t.set("Charges",charges-1);
    		} else {
    			user.message("이 원드는 사용할 수 없는 것 같습니다");
    		}
    		return false;
    	}
    }
    
    private static class DestroyWandScript extends Script {
        private static final long serialVersionUID = 6191533767727822085L;

        public boolean handle(Thing t, Event e) {
            String spell=t.getString("WandSpell");
            Thing s=Spell.create(spell);
            Spell.castAtLocation(s,null,(Map)e.get("DeathMap"),e.getStat("DeathX"),e.getStat("DeathY"));
        	return false;
        }
    }    
    
    private static void initWand(Thing spell) {
        int type=RPG.r(wandtypes.length);
        int adj=RPG.r(wandAdj.length);
        Thing t=Lib.extend("xxx 마법봉","기본 마법봉");

        String spellName=spell.name();
        int spellLvl = spell.getStat("레벨");
        //System.out.println(spellLvl);
        
        if (spell==null) throw new Error("Can't create wand spell: "+spellName);
        
        t.set("이미지",images[type]);
        if (spellLvl > 9) {
        	// add adj if the spell is high enough level
        	t.set("식별안된이름",wandAdj[adj]+" "+wandtypes[type]+" 마법봉");
        } else {
        	t.set("식별안된이름",wandtypes[type]+" 마법봉");
        }
        t.set("이름",spellName+"의 마법봉");
        t.set("이름복수",spellName+"의 마법봉");
        t.set("WandSpell",spellName);
        t.set("최소레벨",RPG.max(1,spell.getLevel()-4));
        t.set("OnUse",new WandScript());
        t.set("OnDeath",new DestroyWandScript());
        
        Lib.add(t);
    }
    
    public static void init() {
        Thing t = Lib.extend("기본 마법봉", "기본 물건");
        t.set("IsWand", 1);
        t.set("IsMagicItem", 1);
        t.set("이미지", 288);
        t.set("HPS", 10);
        t.set("아이템무게", 800);
        t.set("기본가격",500);
        t.set("빈도", 40);
        t.set("Charges",3);
        t.set("아스키코드","/");
        Lib.add(t);
        
        initRandomWands();
    }
    
    private static void initRandomWands() {
        ArrayList spells=Spell.getSpellNames();
    	
        for(int i=0; i<spells.size(); i++) {
        	Thing s=Spell.create((String)spells.get(i));
        	initWand(s);
        }
    }
}